package com.znsb.msfq.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.dialog.UpAppDialog;
import com.znsb.msfq.fragment.Home1Fragment;
import com.znsb.msfq.fragment.Home2Fragment;
import com.znsb.msfq.fragment.Home3Fragment;
import com.znsb.msfq.fragment.Home4Fragment;
import com.znsb.msfq.utils.DeviceUtils;
import com.znsb.msfq.utils.ErrorCodeUtils;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.NetworkAddress;
import com.znsb.msfq.utils.OverallData;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.utils.UrlUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 名称：主页
 * 创建时间：2016/10/20.
 * 作者：杨广
 * 手机号：13330359437
 * 说明：
 */

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private Home1Fragment home1Fragment;
    private Home2Fragment home2Fragment;
    private Home3Fragment home3Fragment;
    private Home4Fragment home4Fragment;
    /**
     * 用于对Fragment进行管理
     */
    private FragmentManager fragmentManager;

    private RadioButton bottom_choose_rb2;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            new UpAppDialog(MainActivity.this, "您有新的版本更新，请去市场更新");
        }
    };


    @Override
    protected int getlayoutView() {
        return R.layout.act_main;
    }

    @Override
    protected void initView(View view) {
        ((RadioGroup) view.findViewById(R.id.bottom_choose_rg)).setOnCheckedChangeListener(this);
        bottom_choose_rb2 = (RadioButton) view.findViewById(R.id.bottom_choose_rb2);
    }

    @Override
    protected void initData() {
        fragmentManager = this.getSupportFragmentManager();
        // 第一次启动时选中第0个tab
        setTabSelection(0);
        UpApp();
    }

    @Override
    protected void clickListener(View view) {

    }


    /**
     * 切换到分类
     */
    public void setTabSelection() {
        bottom_choose_rb2.setChecked(true);

    }

    public void setTabSelection(int index) {
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0:
                if (home1Fragment == null) {
                    // 如果Home1Fragment为空，则创建一个并添加到界面上
                    home1Fragment = new Home1Fragment();
                    home1Fragment.setActivity(MainActivity.this);
                    transaction.add(R.id.ma_fl_layout, home1Fragment);
                } else {
                    // 如果Home1Fragment不为空，则直接将它显示出来
                    transaction.show(home1Fragment);
                }
                break;
            case 1:
                if (home2Fragment == null) {
                    // 如果Home2Fragment为空，则创建一个并添加到界面上
                    home2Fragment = new Home2Fragment();
                    transaction.add(R.id.ma_fl_layout, home2Fragment);
                } else {
                    // 如果Home2Fragment不为空，则直接将它显示出来
                    transaction.show(home2Fragment);
                }
                break;
            case 2:
                if (home3Fragment == null) {
                    // 如果Home3Fragment为空，则创建一个并添加到界面上
                    home3Fragment = new Home3Fragment();
                    transaction.add(R.id.ma_fl_layout, home3Fragment);
                } else {
                    // 如果Home3Fragment不为空，则直接将它显示出来
                    transaction.show(home3Fragment);
                }
                break;
            case 3:
                if (home4Fragment == null) {
                    // 如果Home4Fragment为空，则创建一个并添加到界面上
                    home4Fragment = new Home4Fragment();
                    transaction.add(R.id.ma_fl_layout, home4Fragment);
                } else {
                    // 如果Home4Fragment不为空，则直接将它显示出来
                    transaction.show(home4Fragment);
                }
                break;

        }
        transaction.commit();
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (home1Fragment != null) {
            transaction.hide(home1Fragment);
        }
        if (home2Fragment != null) {
            transaction.hide(home2Fragment);
        }
        if (home3Fragment != null) {
            transaction.hide(home3Fragment);
        }
        if (home4Fragment != null) {
            transaction.hide(home4Fragment);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.bottom_choose_rb1:
                setTabSelection(0);
                break;
            case R.id.bottom_choose_rb2:
                setTabSelection(1);
                break;
            case R.id.bottom_choose_rb3:
                setTabSelection(2);
                break;
            case R.id.bottom_choose_rb4:
                setTabSelection(3);
                break;
            default:
                break;
        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            System.exit(0);
        }
        return false;
    }


    /**
     * Android版本更新
     */
    private void UpApp() {
        Map<String, Object> maps = new HashMap<>();
        maps.put("frameversion", DeviceUtils.getversionName(MainActivity.this));//框架版本号
        maps.put("coreVersion", ""+DeviceUtils.getVersionCode(MainActivity.this));//内核版本号
        maps.put("appKey", OverallData.getChannel());//渠道key(应用市场标识)
        HttpUtils.getInstance(this).postAsnyRequest(NetworkAddress.UpApp, HttpUtils.getMaps(maps),
                new ResponseUtils() {
                    @Override
                    public void onSucceed(String code, JSONObject request) {
                        LogUtils.i("TAG:", "UpApp=" + request);
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onFailure(String result) {
                    }
                });
    }


}
